package com.pmp.service.impl;

import com.pmp.dao.UserDao;
import com.pmp.pojo.ProInfo;
import com.pmp.pojo.ProInfoFile;
import com.pmp.pojo.User;
import com.pmp.service.IUserService;
import com.pmp.util.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * Created by think on 2017/1/2.
 */
@Service("userService")
public class UserServiceImpl implements IUserService {

    @Resource(name = "userDao")
    private UserDao userDao;

    public User login(String username, String pwd) {
        return userDao.login(username,pwd);
    }

    public List<User> queryUser() {
        return userDao.queryUser();
    }

    public void addUser(User user) {
        user.setUserId(UUID.randomUUID().toString());
        user.setPassword(MD5Util.getMD5String(user.getPassword()));
        userDao.insert(user);
    }

    @Override
    public String insertProInfo(ProInfo proInfo,ProInfoFile proInfoFile) {
        String uuid = UUID.randomUUID().toString();
        try{
            proInfo.setId(uuid);
            proInfo.setProjectId(1);
            userDao.insertProInfo(proInfo);
            String[] pathArr = proInfoFile.getPath().split(",");
            if(pathArr.length > 0){
                for(String path : pathArr){
                    if(!"".equals(path)){
                        String uuidFile = UUID.randomUUID().toString();
                        proInfoFile.setId(uuidFile);
                        proInfoFile.setpId(uuid);
                        proInfoFile.setPath(path);
                        userDao.insertProInfoFile(proInfoFile);
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return uuid;
    }

    @Override
    public String insertProInfoFile(ProInfoFile proInfoFile) {
        String uuid = UUID.randomUUID().toString();
        proInfoFile.setId(uuid);
        userDao.insertProInfoFile(proInfoFile);
        return uuid;
    }
}
